using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices.AdomdClient;
using Infragistics.Win.UltraWinTree;


namespace RetailAnalytic.Adhook.PivotTable.Engine
{
    public partial class UCData : UserControl
    {
        public string DefaultServerName = String.Empty;
        public string DefaultServerVersion = String.Empty;
        public string DefaultDBName = String.Empty;
        public string DefaultCubeName = String.Empty;
        public bool IsServerOrLocal = true;
        public AdomdConnection ConnectionAdomdClient;
        public string OlapConstantString = "Provider=MSOLAP.3;";
        public Int16 iCDef = Int16.MinValue;
        public int TreeLevel;

        public UCData()
        {
            InitializeComponent();

        }
        public void SetDefValueNew()
        {
            uce_server.Checked = true;
            uce_server.Enabled = true;
            ute_server.Text = DefaultServerName;
            uce_ver.Text = DefaultServerVersion;
            this.ute_fname.Text = String.Empty;
            ugb_local.Enabled = false;
            this.uce_server.CheckedChanged += new System.EventHandler(this.uce_server_CheckedChanged);
            this.ute_server.ValueChanged += new System.EventHandler(this.ute_server_ValueChanged);
        }
        public void SetDefValue()
        {
            uce_server.Checked = IsServerOrLocal;
            if (IsServerOrLocal)
            {
                ute_server.Text = DefaultServerName;
                uce_ver.Text = DefaultServerVersion;
                this.ute_fname.Text = String.Empty;
                ugb_local.Enabled = false;
                ugb_server.Enabled = true;
            }
            else
            {
                ute_server.Text = "";
                uce_ver.Text = "";
                ute_fname.Text = DefaultServerName;
                ugb_local.Enabled = true;
                ugb_server.Enabled = false;
            }
            uce_server.Enabled = false;
            this.ute_server.ValueChanged += new System.EventHandler(this.ute_server_ValueChanged);
        }
        private void uce_server_CheckedChanged(object sender, EventArgs e)
        {
            IsServerOrLocal = uce_server.Checked;
            if (IsServerOrLocal)
            {
                ute_server.Text = DefaultServerName;
                uce_ver.Text = DefaultServerVersion;
                this.ute_fname.Text = String.Empty;
                ugb_local.Enabled = false;
                ugb_server.Enabled = true;
            }
            else
            {
                ute_server.Text = "";
                uce_ver.Text = "";
                ute_fname.Text = (System.IO.File.Exists(DefaultServerName)) ? DefaultServerName : "";
                ugb_local.Enabled = true;
                ugb_server.Enabled = false;
            }
            try
            {
                if (ParentForm.GetType().Name == "FrmNP")
                {
                    ((FrmNP)ParentForm).FrmNP_Validated(this, EventArgs.Empty);
                }
            }
            catch { }
        }
        private void ultraButton1_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ute_fname.Text = openFileDialog1.FileName;
            }
        }
        public DataTable GetSchemaDataSet_Catalogs(AdomdConnection connection)
        {
            DataTable objTable;
            objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Catalogs, null).Tables[0];
            return objTable;
        }
        public DataTable GetSchemaDataSet_Cubes(AdomdConnection connection, string catalogName, string schemaName, string cubeName)
        {
            DataTable objTable;
            string[] strRestrictions = new string[] { catalogName, schemaName, cubeName };
            objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Cubes, strRestrictions).Tables[0];
            return objTable;
        }
        public DataTable GetSchemaDataSet_Dimensions(AdomdConnection connection, string catalogName, string schemaName, string cubeName, string dimensionName, string dimensionUniqueName)
        {
            DataTable objTable;
            string[] strRestrictions = new string[] { catalogName, schemaName, cubeName, dimensionName, dimensionUniqueName };
            objTable = connection.GetSchemaDataSet(Microsoft.AnalysisServices.AdomdClient.AdomdSchemaGuid.Dimensions, strRestrictions).Tables[0];
            return objTable;
        }
        public bool GetListCubeAdomdClient(string sDb, params string[] sCube)
        {
            try
            {
                ut_ASBrowse.Nodes.Clear();
                TreeLevel = 0;
                iCDef = Int16.MinValue;
                Int16 iC = 0;
                Int16 iCDB = 0;
                DataTable objTableCube;
                Infragistics.Win.UltraWinTree.UltraTreeNode nNode;
                Infragistics.Win.UltraWinTree.UltraTreeNode nParNode;
                ConnectionAdomdClient = new AdomdConnection(OlapConstantString + " Data Source=" + /*ute_server.Text*/ DefaultServerName + ";");
                ConnectionAdomdClient.Open();
                DataTable objTable = GetSchemaDataSet_Catalogs(ConnectionAdomdClient);
                DataRow l_Row;
                ut_ASBrowse.Nodes.Clear();
                nNode = ut_ASBrowse.Nodes.Add();
                nNode.Text = ute_server.Text;
                nNode.Visible = true;
                nNode.Key = "0";
                nParNode = nNode;
                nNode.Override.NodeAppearance.Image = SR.ImagesLibrary.GetImageByName("remote");
                iCDB = 0;
                Application.DoEvents();
                this.Show();
                foreach (DataRow tempLoopVar_l_Row in objTable.Rows)
                {
                    l_Row = tempLoopVar_l_Row;
                    nParNode = ut_ASBrowse.GetNodeByKey("0");
                    nNode = nParNode.Nodes.Add();
                    iC++;
                    iCDB++;
                    nNode.Key = iC.ToString();
                    nNode.Text = l_Row["CATALOG_NAME"].ToString();
                    nNode.Tag = l_Row["CATALOG_NAME"];
                    nNode.Override.NodeAppearance.Image = SR.ImagesLibrary.GetImageByName("database");
                    nParNode = nNode;
                    iC++;   
                    Application.DoEvents();
                    objTableCube = null;
                    ConnectionAdomdClient.ChangeDatabase(l_Row["CATALOG_NAME"].ToString());
                    objTableCube = GetSchemaDataSet_Cubes(ConnectionAdomdClient, l_Row["CATALOG_NAME"].ToString(), null, null);
                    if (!(objTableCube == null))
                    {
                        DataRow l_RowC;
                        foreach (DataRow tempLoopVar_l_RowC in objTableCube.Rows)
                        {
                            l_RowC = tempLoopVar_l_RowC;
                            nNode = nParNode.Nodes.Add();
                            iC++;
                            nNode.Key = iC.ToString();
                            nNode.Text = l_RowC["CUBE_NAME"].ToString();
                            nNode.Override.NodeAppearance.Image = SR.ImagesLibrary.GetImageByName("cube");
                            if (sCube.Length > 0)
                            {
                                if (sDb == "")
                                    sDb = l_RowC["CUBE_NAME"].ToString();
                                if (l_RowC["CUBE_NAME"].ToString().ToLower() == sCube[0].ToLower() && l_Row["CATALOG_NAME"].ToString().ToLower() == sDb.ToLower())
                                {
                                    iCDef = iC;
                                }
                            }

                            DataTable objTableD = GetSchemaDataSet_Dimensions(ConnectionAdomdClient, l_Row["CATALOG_NAME"].ToString(), null, l_RowC["CUBE_NAME"].ToString(), null, null);
                            if (!(objTableD == null))
                            {
                                DataRow l_RowD;
                                foreach (DataRow tempLoopVar_l_RowD in objTableD.Rows)
                                {
                                    l_RowD = tempLoopVar_l_RowD;
                                    if (l_RowD["DIMENSION_NAME"].ToString().ToLower() == "objGeo".ToLower())
                                    {
                                        nNode.Override.NodeAppearance.Image = SR.ImagesLibrary.GetImageByName("cube");
                                        nNode.Tag = "MapCube";
                                    }
                                }
                            }
                        }
                    }
                    Application.DoEvents();
                }
                ut_ASBrowse.GetNodeByKey(iCDef.ToString()).Parent.Expanded = true;
                ut_ASBrowse.GetNodeByKey(iCDef.ToString()).Expanded = true;
                ut_ASBrowse.GetNodeByKey(iCDef.ToString()).Selected = true;
                DefaultCubeName = ut_ASBrowse.GetNodeByKey(iCDef.ToString()).Text;
                ConnectionAdomdClient.ChangeDatabase(sDb);
                ut_ASBrowse.GetNodeByKey("0").Expanded = true;
                ut_ASBrowse.Focus();
                return true;
            }
            catch (Exception ex)
            {
                RetailAnalytic.Core.Dialog.Show(RetailAnalytic.Core.Dialog_type.Error,SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Error_NO_FOUND,
                    Environment.StackTrace, "") +
                    SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Inf_DB_NAME_P1, Environment.StackTrace, "", new string[] { sDb }) +
                    SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Inf_SERVER_P1, Environment.StackTrace, "", new string[] { DefaultServerName }) +
                    SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Inf_CUBE_P1, Environment.StackTrace, "", new string[] { DefaultCubeName }));
                return false;
            }
        }
        private void ultraButton2_Click(object sender, EventArgs e)
        {
            DefaultServerName = ute_server.Text;
            GetListCubeAdomdClient(DefaultDBName, new string[1] { DefaultCubeName }); //(DefDbName);
        }
        private void ut_ASBrowse_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
        {
            UltraTreeNode utn;
            try
            {
                utn = e.NewSelections[0];
                TreeLevel = utn.Level;
                if (utn.Level == 2)
                {
                    ConnectionAdomdClient.ChangeDatabase(utn.Parent.Text);
                    DefaultCubeName = utn.Text;
                }
                else
                {
                    ConnectionAdomdClient.ChangeDatabase(utn.Text);
                }
            }
            catch { }
            try
            {
                if (ParentForm.GetType().Name == "FrmNP")
                {
                    ((FrmNP)ParentForm).FrmNP_Validated(this, EventArgs.Empty);
                }
            }
            catch { }

        }
        private void ute_server_ValueChanged(object sender, EventArgs e)
        {
            if (!uce_server.Enabled)
            {
                ut_ASBrowse.Nodes.Clear();
            }
        }

        private void ute_fname_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (ParentForm.GetType().Name == "FrmNP")
                {
                    ((FrmNP)ParentForm).FrmNP_Validated(this, EventArgs.Empty);
                }
            }
            catch { }

        }
    }
}
